package org.example.struct.array;

/**
 * @author liyishan
 * @date 2023/12/28 09:34
 * @apiNote
 */

public class Test00392IsSubsequence {
    public static boolean isSubsequence(String s, String t) {
        if("".equals(s)){
            return true;
        }
        int len = 0;
        for (int i = 0; i < t.length(); i++) {
            if(s.charAt(len) == t.charAt(i)){
                len++;
                if(len == s.length()){
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean isSubsequence1(String s, String t) {
        int m = s.length(), n = t.length();
        int i = 0, j = 0;
        while (i < m && j < n) {
            if (s.charAt(i) == t.charAt(j)) {
                i++;
            }
            j++;
        }
        return i == m;
    }


    public static boolean isSubsequence2(String s, String t) {
        int n = s.length(), m = t.length();
        int[][] dp = new int[m + 1][26];
        for (int i = 0; i < 26; i++) {
            dp[m][i] = m;
        }
        for (int i = m - 1; i >= 0; i--) {
            for(int j = 0;j < 26;j++){
                if(t.charAt(i) == j + 'a'){
                    dp[i][j] = i;
                }else{
                    dp[i][j] = dp[i + 1][j];
                }
            }
        }
        int add = 0;
        for (int i = 0; i < n; i++) {
            if(dp[add][s.charAt(i) - 'a'] == m){
                return false;
            }
            add = dp[add][s.charAt(i) - 'a'] + 1;
        }
        return true;
    }
}
